<?php
namespace models\service;
//use entities;

class CategoryService {

	protected $ci;
	public $invLastId;
	
	public function __construct() {
		$this -> ci = &get_instance();
	}

	public function findAllCategory($orderBy=null){
		return $this -> ci -> em -> getRepository('\models\entities\Category') -> findAll($orderBy);
	}
	
	public function findCategoryByParentId($id){
		$criteria = array('parent'=>$id);
		return $this->ci->em->getRepository('\models\entities\Category') -> findBy($criteria);
	}
	
	public function findBy($criteria){
		return $this->ci->em->getRepository('\models\entities\Category')->findBy($criteria);
	}
	
	public function findOneBy($criteria){
		return $this->ci->em->getRepository('\models\entities\Category')->findOneBy($criteria);
	}

	public function save($param){
		if (array_key_exists("id", $param)){			
			if ($param['id'] > 0){
				$category = $this->findOneBy(array("id"=>$param['id']));
			} else {					
				$category = new \models\entities\Category;		
			}
		} else {
			$category = new \models\entities\Category;
		}
		
		$category->setName($param['name']);
		if (array_key_exists("parent", $param)){
			if ($param['parent'] >= 0){
				$parent = $this->ci->em->getRepository('\models\entities\Category')->findOneBy(array('id' => $param['parent']));
				$category->setParent($parent);	
			} 				
		}
		
		
		
		$createdDate = new \DateTime("now", new \DateTimeZone("Asia/Jakarta"));	
		$category->setCreatedDate($createdDate);
		try {
			$this -> ci -> em -> persist($category);
			$this -> ci -> em -> flush();
			
			if (array_key_exists("level", $param)){
				$categoryPath = new \models\entities\CategoryPath;
				$categoryPath->setCategory($category);
				$categoryPath->setPath($param['level']."/".$category->getId());
				$this -> ci -> em -> persist($categoryPath);
				$this -> ci -> em -> flush();
			}
			return $category;
		} catch (Exception $e){
			return NULL;	
		}		
	}
	
	public function remove($id, $force=FALSE){
		$result['result'] = FALSE;
		$result['message'] = "No Operation"; 
		
		$criteria = array('id' => $id);
		$category = $this->findOneBy($criteria);
		
		$childs0 = $this->findBy(array('parent'=>$id));
		
		if (count($childs0) > 0){		
			if ($force == "FALSE"){
				$result['result'] = FALSE;
				$result['message'] = "This category has sub-categories";
				return $result;	
			} else {				
				$childs = $this->ci->em->getRepository('\models\entities\Category')->findChildOfCategory($id);												
			}			
		}
		
		try {
			if (count($childs) > 0 && $force != "FALSE"){
				
				foreach ($childs as $child){
					//$this -> ci -> em -> persist($child);
					$this -> ci -> em -> remove($child);
					$this -> ci -> em -> flush();	
				}				
			}
			$this -> ci -> em -> remove($category);
			$this -> ci -> em -> flush();	
			
			$result['result'] = TRUE;
			return $result;
			//$result['message'] = "Success";
		} catch (exception $e){
			if (!$force){
				$result['result'] = FALSE;
				$result['message'] = $e->getMessage();
				return $result;
			} 
		}
		
		
	}
}
